package co.edu.unal.bioingenium.kbmed.features;

import gnu.trove.map.TIntFloatMap;
import gnu.trove.map.hash.TIntFloatHashMap;
import java.io.Serializable;
import java.util.Arrays;

/**
 *
 * @author Alejandro Riveros Cruz
 */
public class CompactFeatureVector implements Serializable {

    //
    private static final long serialVersionUID = 2998810632892632675L;
    //
    private TIntFloatMap features;
    private int[] sortedKeys;

    /**
     */
    public CompactFeatureVector() {
        features = new TIntFloatHashMap();
    }

    /**
     * @param values 
     */
    public CompactFeatureVector(float[] values) {
        features = new TIntFloatHashMap();
        float value;
        for (int i = 0; i < values.length; i++) {
            value = values[i];
            if (value != 0) {
                features.put(i, value);
            }
        }
    }

    /**
     * @param wordID 
     * @param value 
     */
    public void setValue(int wordID, float value) {
        features.put(wordID, value);
    }

    /**
     * @param wordID 
     * @return the docID
     */
    public float getValue(int wordID) {
        return features.get(wordID);
    }

    /**
     * @param length 
     * @return the docID
     */
    public float[] getExpandedFeatureVector(int length) {
        float[] feature = new float[length];
        int[] keys = features.keys();
        for (int i = 0; i < keys.length; i++) {
            feature[keys[i]] = features.get(keys[i]);
        }
        return feature;
    }

    /**
     *
     */
    public void normalize() {
        float total = 0;
        int[] keys = features.keys();
        for (int i = 0; i < keys.length; i++) {
            total += features.get(keys[i]);
        }
        for (int i = 0; i < keys.length; i++) {
            features.put(keys[i], features.get(keys[i]) / total);
        }
    }

    /**
     *
     * @return 
     */
    public int[] getSortedIndexKeys() {
        if (sortedKeys == null || sortedKeys.length != features.size()) {
            sortedKeys = features.keys();
            Arrays.sort(sortedKeys);
        }
        return sortedKeys;
    }

    /**
     *
     * @return 
     */
    public int[] getIndexKeys() {
        return features.keys();
    }

    /**
     * @return the numFeatures
     */
    public int getFeaturesLength() {
        return features.size();
    }
    
}
